<?php


namespace app\admin\service\base;


use app\admin\model\base\QuestionCate;
use app\admin\validates\PaperCateValidate;
use app\admin\validates\QuestionCateValidate;
use app\BaseService;
use think\exception\ValidateException;
use think\response\Json;

class QuestionCateService extends BaseService
{
    /**
     * 班级列表
     * @param array $params
     * @return Json
     */
    public function index(array $params): Json
    {
        $where = ['is_del'=>0];
        //获取列表
        $data = QuestionCate::getList($where, $params['page'] ?? 1, $params['limit'] ?? 100, 'id desc');
        return jsonReturn(1, $data);
    }

    /**
     * 详情
     * @param array $params
     * @return Json
     */
    public function read(array $params): Json
    {
        //数据验证
        try {
            validate(QuestionCateValidate::class)->scene('details')->check($params);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return jsonReturn(0, [], $e->getError());
        }
        $where = ['id'=>$params['id']];
        $data = QuestionCate::findOne($where);
        return jsonReturn(1, $data);
    }

    /**
     * 保存班级
     * @param array $params
     * @return Json
     */
    public function save(array $params): Json
    {
        //数据验证
        try {
            validate(QuestionCateValidate::class)->scene('add')->check($params);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return jsonReturn(0, [], $e->getError());
        }
        $data = QuestionCate::create(['name'=>$params['name'],'sort'=>$params['sort']]);
        if($data){
            return jsonReturn(1, []);
        }
        return jsonReturn(0, []);
    }

    /**
     * 编辑班级
     * @param array $params
     * @return Json
     */
    public function update(array $params): Json
    {
        //数据验证
        try {
            validate(QuestionCateValidate::class)->scene('edit')->check($params);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return jsonReturn(0, [], $e->getError());
        }
        $data = QuestionCate::where('id','=',$params['id'])->update(['name'=>$params['name'],'sort'=>$params['sort']]);
        if($data){
            return jsonReturn(1, []);
        }
        return jsonReturn(0, []);
    }

    /**
     * 编辑班级
     * @param array $params
     * @return Json
     */
    public function delete(array $params): Json
    {
        //数据验证
        try {
            validate(QuestionCateValidate::class)->scene('del')->check($params);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return jsonReturn(0, [], $e->getError());
        }
        $data = QuestionCate::where('id','=',$params['id'])->update(['is_del'=>1]);
        if($data){
            return jsonReturn(1, []);
        }
        return jsonReturn(0, []);
    }
}